import 'package:flutter/material.dart';
import 'package:flutter_base_library/app/themes/app_colors.dart';
import 'package:flutter_base_library/index.dart';
import 'package:get/get.dart';
import 'package:safe_manage/widgets/event_status_tag.dart';
import 'package:safe_manage/widgets/info_content_item.dart';
import 'alarm_info_controller.dart';

class AlarmInfoPage extends StatelessWidget {
  final controller = Get.find<AlarmInfoController>();
  AlarmInfoPage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("事件详情"),
      ),
      body: Container(
        width: Get.width,
        color: const Color(0xFFEFEFF4),
        child: Column(
          children: [
            Expanded(
              child: _bodyContent(context),
            ),
            Obx(
              () => Visibility(
                visible: controller.event.value?.sfcl == 0,
                child: _bottomContent(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _bodyContent(BuildContext context) {
    return ListView(
      children: [
        _topContent(),
        _infoListWidget(),
        _eventContent(),
        Visibility(
          visible: controller.type != 8,
          child: _pictureContent(context),
        ),
        Visibility(
          visible: controller.type != 8,
          child: _videoContent(context),
        ),
        const SizedBox(height: 100),
      ],
    );
  }

  Widget _topContent() {
    return Container(
      margin: const EdgeInsets.fromLTRB(0, 10, 0, 10),
      padding: const EdgeInsets.fromLTRB(15, 0, 20, 0),
      height: 50,
      color: Colors.white,
      child: Row(
        children: [
          Expanded(
            child: Text(
              "${controller.titleList[controller.type]}记录详情",
              style: const TextStyle(
                color: Color(0xFF101010),
                fontSize: 18,
              ),
            ),
          ),
          const SizedBox(width: 5),
          Obx(() {
            return EventStatusTag(
              status: controller.event.value?.sfcl,
            );
          }),
        ],
      ),
    );
  }

  Widget _infoListWidget() {
    return Container(
      padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
      width: Get.width,
      color: Colors.white,
      child: Column(
        children: [
          InfoContentItem("部门名称", controller.commonRecord.cjbmmc ?? ""),
          _infoWidget(),
          InfoContentItem("备注", controller.commonRecord.ms ?? ""),
        ],
      ),
    );
  }

  Widget _infoWidget() {
    switch (controller.type) {
      case 0:
      // return _infoContent0();
      case 1:
      // return _infoContent1();
      case 2:
      // return _infoContent2();
      case 3:
      // return _infoContent3();
      case 6:
        return _infoContent0();
      case 4:
        return _infoContent4();
      case 5:
        return _infoContent5();
      case 8:
        return _infoContent8();
      case 9:
        return _infoContent9();
      default:
        return Container();
    }
  }

  // 0抽烟检测 1电子围栏 2电话检测 3烟火检测 6车顶作业
  Widget _infoContent0() {
    return Column(
      children: [
        InfoContentItem("任务名称", controller.commonRecord.rwmc ?? ""),
        InfoContentItem("设备名称", controller.commonRecord.camera?.sbmc ?? ""),
        InfoContentItem("设备IP", controller.commonRecord.camera?.sbip ?? ""),
        InfoContentItem("总数", controller.commonRecord.total?.toString() ?? ""),
        InfoContentItem(
            "目标总数", controller.commonRecord.noTotal?.toString() ?? ""),
        InfoContentItem(
          "报警状态",
          () {
            if (controller.commonRecord.sbjg == 1) {
              return "正常";
            }
            if (controller.commonRecord.sbjg == 2) {
              return "报警";
            }
            return "";
          }(),
        ),
        InfoContentItem("设备 SIP", controller.commonRecord.camera?.sipId ?? ""),
        InfoContentItem("置信度", controller.commonRecord.score?.toString() ?? ""),
        InfoContentItem(
          "事件时间",
          () {
            int? milliseSecond = controller.commonRecord.sjsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "----/--/-- --:--:--";
          }(),
        ),
        InfoContentItem(
          "创建时间",
          () {
            int? milliseSecond = controller.commonRecord.cjsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond * 1000,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "----/--/-- --:--:--";
          }(),
        ),
      ],
    );
  }

  // 装卸油
  Widget _infoContent4() {
    return Column(
      children: [
        InfoContentItem("任务名称", controller.commonRecord.rwmc ?? ""),
        InfoContentItem(
          "创建时间",
          () {
            int? milliseSecond = controller.commonRecord.cjsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond * 1000,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "----/--/-- --:--:--";
          }(),
        ),
        InfoContentItem(
          "修改时间",
          () {
            int? milliseSecond = controller.commonRecord.xgsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond * 1000,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "----/--/-- --:--:--";
          }(),
        ),
      ],
    );
  }

  // 加油站巡检
  Widget _infoContent5() {
    return Column(
      children: [
        InfoContentItem("任务名称", controller.commonRecord.rwmc ?? ""),
        Obx(() => InfoContentItem(
              "巡检总数(实际/计划)：",
              controller.patrolModel.value?.xjzs?.toString() ?? "",
              titleWidth: 180,
            )),
        Obx(() => InfoContentItem(
              "上午总数(实际/计划)：",
              controller.patrolModel.value?.swsl?.toString() ?? "",
              titleWidth: 180,
            )),
        Obx(() => InfoContentItem(
              "下午总数(实际/计划)：",
              controller.patrolModel.value?.xwsl?.toString() ?? "",
              titleWidth: 180,
            )),
        Obx(
          () => InfoContentItem(
            "夜间总数(实际/计划)：",
            controller.patrolModel.value?.yjsl?.toString() ?? "",
            titleWidth: 180,
          ),
        ),
        InfoContentItem(
          "创建时间",
          () {
            int? milliseSecond = controller.commonRecord.cjsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond * 1000,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "----/--/-- --:--:--";
          }(),
        ),
        InfoContentItem(
          "修改时间",
          () {
            int? milliseSecond = controller.commonRecord.xgsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond * 1000,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "----/--/-- --:--:--";
          }(),
        ),
      ],
    );
  }

  // 设备状态
  Widget _infoContent8() {
    return Column(
      children: [
        InfoContentItem("设备名称", controller.commonRecord.camera?.sbmc ?? ""),
        InfoContentItem("设备IP", controller.commonRecord.camera?.sbip ?? ""),
        InfoContentItem(
            "设备状态", controller.commonRecord.sbjg == 1 ? "在线" : "离线"),
        InfoContentItem("设备SIP", controller.commonRecord.camera?.sipId ?? ""),
        InfoContentItem(
          "离线时间",
          () {
            int? milliseSecond = controller.commonRecord.lxsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "";
          }(),
        ),
        InfoContentItem(
          "上线时间",
          () {
            int? milliseSecond = controller.commonRecord.sxsj?.toInt();
            if (milliseSecond != null && milliseSecond != 0) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "";
          }(),
        ),
        InfoContentItem(
          "创建时间",
          () {
            int? milliseSecond = controller.commonRecord.cjsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond * 1000,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "";
          }(),
        ),
        // InfoContentItem("备注", controller.commonRecord.ms ?? ""),
      ],
    );
  }

  Widget _infoContent9() {
    return Column(
      children: [
        InfoContentItem("设备名称", controller.commonRecord.camera?.sbmc ?? ""),
        InfoContentItem("任务名称", controller.commonRecord.rwmc ?? ""),
        InfoContentItem("设备IP", controller.commonRecord.camera?.sbip ?? ""),
        InfoContentItem("设备SIP", controller.commonRecord.camera?.sipId ?? ""),
        InfoContentItem(
          "报警时间",
          () {
            int? milliseSecond = controller.commonRecord.sjsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "";
          }(),
        ),
        InfoContentItem(
          "创建时间",
          () {
            int? milliseSecond = controller.commonRecord.cjsj?.toInt();
            if (milliseSecond != null) {
              DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                milliseSecond * 1000,
              );
              return toLocalDateTimeByDateTime(dateTime);
            }
            return "";
          }(),
        ),
        // InfoContentItem("备注", controller.commonRecord.ms ?? ""),
      ],
    );
  }

  Widget _eventContent() {
    return Obx(
      () => Visibility(
        visible:
            controller.event.value?.sfcl != 0 && controller.event.value != null,
        child: Container(
          margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
          padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
          color: Colors.white,
          child: Column(
            children: [
              InfoContentItem(
                "处理状态",
                [
                  "未处理",
                  "已处理",
                  "误报"
                ][controller.event.value?.sfcl?.toInt() ?? 0],
              ),
              InfoContentItem("处理人", controller.event.value?.clyhxm ?? ""),
              InfoContentItem(
                "处理时间",
                () {
                  int? milliseSecond = controller.event.value?.clsj?.toInt();
                  if (milliseSecond != null) {
                    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(
                      milliseSecond * 1000,
                    );
                    return toLocalDateTimeByDateTime(dateTime);
                  }
                  return "----/--/-- --:--:--";
                }(),
              ),
              InfoContentItem("处理意见", controller.event.value?.bjlx ?? ""),
            ],
          ),
        ),
      ),
    );
  }

  Widget _pictureContent(BuildContext context) {
    return Obx(
      () => Container(
        margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
        padding: const EdgeInsets.fromLTRB(15, 10, 15, 20),
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              "记录截图",
              style: TextStyle(
                color: Color(0xFF6C6C6C),
                fontSize: 18,
              ),
            ),
            const SizedBox(height: 10),
            controller.imgStrList.isEmpty
                ? Row(
                    children: [
                      Container(
                        // padding: const EdgeInsets.fromLTRB(5, 0, 5, 0),
                        child: controller.imgUrl.value == null
                            ? Container()
                            : GestureDetector(
                                onTap: () {
                                  controller.showImgDetails(context);
                                },
                                child: Image.network(
                                  controller.imgUrl.value!,
                                  fit: BoxFit.fill,
                                  width: 70,
                                  height: 70,
                                ),
                              ),
                      ),
                    ],
                  )
                : SizedBox(
                    height: 70,
                    child: Row(
                      children: [
                        Expanded(
                          child: ListView.separated(
                            itemBuilder: (_, index) {
                              return GestureDetector(
                                onTap: () {
                                  controller.showImgsDetails(context, index);
                                },
                                behavior: HitTestBehavior.translucent,
                                child: Image.network(
                                  controller.imgStrList[index],
                                  fit: BoxFit.fill,
                                  width: 70,
                                  height: 70,
                                ),
                              );
                            },
                            separatorBuilder: (_, index) {
                              return const SizedBox(width: 10);
                            },
                            itemCount: controller.imgList.length,
                            scrollDirection: Axis.horizontal,
                          ),
                        ),
                        GestureDetector(
                          onTap: () {
                            controller.morePictureClicked(context);
                          },
                          behavior: HitTestBehavior.translucent,
                          child: Row(
                            children: const [
                              SizedBox(width: 5),
                              Text(
                                "更多",
                                style: TextStyle(
                                  color: Color(0xFF4095E5),
                                  fontSize: 12,
                                ),
                              ),
                              SizedBox(
                                // width: 50,
                                height: 50,
                                child: Icon(Icons.arrow_forward_ios),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
            const SizedBox(height: 10),
            const Text(
              "点击查看大图",
              style: TextStyle(
                color: Color(0xFF9A9A9A),
                fontSize: 14,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _videoContent(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
      padding: const EdgeInsets.fromLTRB(15, 10, 15, 20),
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            "记录视频",
            style: TextStyle(
              color: Color(0xFF6C6C6C),
              fontSize: 18,
            ),
          ),
          const SizedBox(height: 10),
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // const Icon(
              //   Icons.video_file,
              //   size: 40,
              //   color: AppColors.kPrimaryColor,
              // ),
              // const SizedBox(width: 10),
              Expanded(
                child: GridView.builder(
                  shrinkWrap: true,
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    // mainAxisSpacing: 10,
                    crossAxisSpacing: 10,
                    mainAxisExtent: 40,
                  ),
                  itemCount: controller.videoList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return GestureDetector(
                      onTap: () {
                        controller.videoClicked(index);
                      },
                      behavior: HitTestBehavior.translucent,
                      child: Container(
                        margin: const EdgeInsets.fromLTRB(0, 5, 0, 5),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(5),
                          border: Border.all(color: AppColors.kPrimaryColor),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          "查看视频${index + 1}",
                          style: const TextStyle(
                            color: Color(0xFF101010),
                            fontSize: 14,
                          ),
                        ),
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _bottomContent() {
    return Container(
      padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
      width: Get.width,
      height: 64,
      // color: Colors.white,
      child: ElevatedButton(
        onPressed: () {
          controller.bottomButtonClicked();
        },
        child: const Text(
          "报警处理",
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w400,
          ),
        ),
      ),
    );
  }
}
